
package com.hsy.crm.web.domain.entity;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.Validate;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import tk.mybatis.mapper.entity.IDynamicTableName;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.text.ParseException;
import java.util.Date;

/**
 * 修改属性需要修改baseService.isBaseEntityProperty方法
 */
public class BaseEntity implements IDynamicTableName{

    @Id
    @Validate(update=true,delete = true)
    private Long id;

    /**
     * 不体现在表结构上 体现在表名上
     */
    @Transient
    private String customerId;

    @Transient
    private Integer page = 1;

    @Transient
    private Integer limit = 10;

    @Transient
    private Long loginUserId;

    @Transient
    private Long loginCompanyId;

    @Transient
    @Validate(insert=false)
    private String loginCompanyLogicalCode;

    @Transient
    @Validate(insert=false)
    private String examRoles;

    public Long getLoginCompanyId() {
        return loginCompanyId;
    }

    public void setLoginCompanyId(Long loginCompanyId) {
        this.loginCompanyId = loginCompanyId;
    }

    public boolean isPurchaseAdmin(){
        return exam(App.ExamRoles.CG);
    }

    public boolean isSaleAdmin(){
        return exam(App.ExamRoles.XS);
    }

    public boolean isStoneAdmin(){
        return exam(App.ExamRoles.CK);
    }

    public boolean isFinanceAdmin(){
        return exam(App.ExamRoles.CW);
    }

    public boolean isBaseAdmin(){
        return exam(App.ExamRoles.GLY);
    }

    private boolean exam(String role){
        if(StringUtils.isEmpty(examRoles)){
            return false;
        }

        String[] exams = examRoles.split(",");
        for ( String exam : exams ){
            if( StringUtils.equals(exam,role) ){
                return true;
            }
        }
        return false;
    }

    public String getExamRoles() {
        return examRoles;
    }

    public void setExamRoles(String examRoles) {
        this.examRoles = examRoles;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Long getLoginUserId() {
        return loginUserId;
    }

    public void setLoginUserId(Long loginUserId) {
        this.loginUserId = loginUserId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    public String getLoginCompanyLogicalCode() {
        return loginCompanyLogicalCode;
    }

    public void setLoginCompanyLogicalCode(String loginCompanyLogicalCode) {
        this.loginCompanyLogicalCode = loginCompanyLogicalCode;
    }

    @Override
    public String getDynamicTableName() {
        Class<?> clazz = this.getClass();
        Table table = clazz.getAnnotation(Table.class);
        if( table != null ){
            return table.name()+"_"+this.getCustomerId();
        }else{
            Class<?> superClazz = clazz.getSuperclass();
            table = superClazz.getAnnotation(Table.class);
            if( table != null ){
                return table.name()+"_"+this.getCustomerId();
            }else{
                return null;
            }
        }
    }

    @Transient
    private static final String PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static Date parse(String value){
        try {
            return DateUtils.parseDate(value,PATTERN);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
